package com.dmw.controller;

import com.dmw.dto.Dto;
import com.dmw.entity.DmUser;
import com.dmw.service.UserConsumerService;
import com.dmw.util.*;
import com.dmw.vo.userBuyTicketsVO.AddBuyTicketsVo;
import com.dmw.vo.userInfoPageVO.ModifyUserInfoVo;
import com.dmw.vo.userLoginVO.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping(value = "user/api")
public class UserController {

    @Autowired
    private UserConsumerService userConsumerService;
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private RedisUtil redisUtil;

    //邮件激活码有效期
    private int timeForMail = 2;

    //激活码前缀
    private String active_prefix = "active:";

    /**
     * 获取注册验证码接口
     * @param map
     * @return
     */
    @PostMapping("/p/code")
    @ResponseBody
    public Dto code(@RequestBody Map<String, Object> map){
        System.out.println(">>>获取注册验证码接口");
        System.out.println(active_prefix + map.get("phone"));
        if (redisUtil.hasKey(active_prefix + map.get("phone")))
            return DtoUtil.returnFail("验证码已发送！请勿频繁操作。","1002");
        //发送激活码
        String code="";
        try {
            /* 发送邮件到用户邮箱 */
            //生成激活码
            code = MD5.getMd5(new Date().toString(), 6);
            //发送激活码到用户邮箱
            mailUtil.send(String.valueOf(map.get("phone")), "986208738@qq.com",
                    "注册大密网账号", "请在" + timeForMail + "分钟内激活账号，激活码：" + code);

            //将激活码保存到redis
            redisUtil.setString(active_prefix + (map.get("phone")), code, timeForMail * 60);
                return DtoUtil.returnSuccess("发送邮件成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("验证码已发送！请勿频繁操作。","1002");
    }

    /**
     * 验证手机号是否可用接口
     * @param map
     * @return
     */
    @PostMapping("/p/checkEmail")
    @ResponseBody
    public Dto checkEmail(@RequestBody Map<String, Object> map){
        System.out.println(">>>验证手机号是否可用接口");
        System.out.println(map.get("phone"));

        //添加用户
        return userConsumerService.checkEmail(map);
    }

    /**
     * 使用邮箱/手机号注册接口
     * @param map
     * @return
     */
    @PostMapping("/p/register")
    @ResponseBody
    public Dto register(@RequestBody Map<String, Object> map){
        System.out.println(">>>registe");
        System.out.println(map);
        //添加用户
        return  userConsumerService.register(map);
    }

    /**
     * 根据当前用户返回当前用户常用购票人列表
     * from 常用购票人页面 1
     * @return
     */
    @RequestMapping("/v/queryLinkUser")
    @ResponseBody
    public Dto queryLinkUser(HttpServletRequest request){
        System.out.println(">>>根据当前用户返回当前用户常用购票人列表");
        String token = request.getHeader("token");
        System.out.println(token);
        Map<String, Object> map=new HashMap<>();
        map.put("token",token);
        return userConsumerService.queryLinkUser(map);

    }

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/p/login")
    @ResponseBody
    public Dto login(@RequestBody LoginVO vo){
        System.out.println(">>>用户登录");
        System.out.println(vo.getPhone());
        System.out.println(vo.getPassword());
        Map<String, Object> map=new HashMap<>();
        map.put("phone",vo.getPhone());
        map.put("password",vo.getPassword());
        return userConsumerService.login(map);
    }

    /**
     * 根据用户token返回用户信息
     * from 个人信息页面接口 1
     * @return
     */
    @RequestMapping("/v/queryUserInfoByToken")
    public Dto queryUserInfoByToken(HttpServletRequest request) {
        System.out.println(">>>根据用户token返回用户信息");
        String token = request.getHeader("token");
        Map<String, Object> map=new HashMap<>();
        map.put("token",token);
        return userConsumerService.queryUserInfoByToken(map);
    }

    /**
     * 修改用户信息
     * from 个人信息页面接口 2
     * @return
     */
    @RequestMapping("/v/modifyUserInfo")
    public Dto modifyUserInfo(@RequestBody ModifyUserInfoVo vo,HttpServletRequest request) {
        System.out.println(">>>修改用户信息");
        System.out.println(vo.toString());
        String token = request.getHeader("token");
        Map<String, Object> map=new HashMap<>();
        map.put("token",token);
        map.put("nickName",vo.getNickName());
        map.put("phone",vo.getPhone());
        map.put("realName",vo.getRealName());
        map.put("sex",vo.getSex());
        map.put("idCard",vo.getIdCard());
        map.put("birthday",vo.getBirthday());
        map.put("hobby",vo.getHobby());
        map.put("imgUrl",vo.getImgUrl());
        return userConsumerService.modifyUserInfo(map);
    }

    /**
     * 添加常用购票人
     * from 常用购票人页面 2
     * @return
     */
    @RequestMapping("/v/addLinkUser")
    public Dto addLinkUser(@RequestBody AddBuyTicketsVo vo,HttpServletRequest request) {
        System.out.println(">>>添加常用购票人");
        System.out.println(vo.toString());
        String token = request.getHeader("token");
        Map<String, Object> map=new HashMap<>();
        map.put("token",token);
        map.put("name",vo.getName());
        map.put("idCard",vo.getIdCard());
        map.put("cardType",vo.getCardType());
        return userConsumerService.addLinkUser(map);
    }

    /**
     * 验证购票人是否已经存在
     * from 常用购票人页面 3
     * @return
     */
    @RequestMapping("/v/queryLinkUserByIdCard")
    public Dto queryLinkUserByIdCard(@RequestBody Map<String, Object> map,HttpServletRequest request) {
        System.out.println(">>>验证购票人是否已经存在");
        System.out.println(map.get("isCard"));
        String token = request.getHeader("token");
        map.put("token",token);
        return userConsumerService.queryLinkUserByIdCard(map);

    }

    /**
     * 删除联系人
     * from 常用购票人页面 4
     * @return
     */
    @RequestMapping("/v/deleteLinkUserById")
    public Dto deleteLinkUserById(@RequestBody Map<String, Object> map,HttpServletRequest request) {
        System.out.println(">>>删除联系人");
        System.out.println(map.get("id"));
        String token = request.getHeader("token");
        map.put("token",token);
        return userConsumerService.deleteLinkUserById(map);
    }
}
